//
//  JWShopHomeTableVC.m
//  jinwang
//
//  Created by jzb on 2018/4/10.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWShopHomeTableVC.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "JWShopHomeCommodityCell.h"
#import "JWFBFacadeVCModel.h"

@interface JWShopHomeTableVC ()<SDCycleScrollViewDelegate>
{
    
    NSInteger classifyListBGViewHeight;
}
@property (nonatomic,strong) UIView *bannerBGView,*classifyListBGView;//轮播图背景 分类Btn背景
@property (nonatomic,strong) SDCycleScrollView *banner;//轮播图
@property (nonatomic,strong)JWShopHomeCommodityCell *cell;

/* 数据源 */
@property (nonatomic,strong) NSMutableArray *source_banner,*classifyListArr,*sortBtnArr;

@end

@implementation JWShopHomeTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpTableView];
    [self HSFNet_getBannerInfo];
    [self makeUrl_mall_indexList];
}
//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWShopHomeCommodityCell class])]];
    self.tableView.tableHeaderView = self.bannerBGView;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self HSFNet_getBannerInfo];
        [self makeUrl_mall_indexList];
//            [self makeUrl_mall_classifyList];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[JWShopHomeCommodityCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
//    [self.cell.GeRenZiLiaoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
//    [self.cell.MyWalletBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
//    [self.cell.MyCollectBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
//    [self.cell.BackCardBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
//    [self.cell.ConfigurationBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.cell.zhaocheBut addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}
-(SDCycleScrollView *)banner{
    if (!_banner) {
        _banner =  [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth * (1.00/2.00)) delegate:self placeholderImage:[UIImage imageNamed:@""]];
        _banner.currentPageDotColor = [UIColor whiteColor]; // 自定义当前分页控件小圆标颜色
        _banner.pageDotColor = kRGBColor(161, 161, 161);  // 自定义其他分页控件小圆标颜色
        _banner.backgroundColor = [UIColor whiteColor];
        _banner.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _banner.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
        _banner.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _banner.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _banner.autoScrollTimeInterval = 3.0;
        _banner.hidesForSinglePage = NO;
        _banner.autoScroll = YES;
    }
    return _banner;
}

-(UIView *)bannerBGView{
    
     _classifyListBGView = [[UIView alloc]init];
    NSInteger x = 0;
    NSInteger y = 0;
    for (NSInteger i=0; i<_classifyListArr.count; i++) {
        JWFBFacadeVCModel *model = _classifyListArr[i];
        if (x + kScreenWidth/5 > self.view.frame.size.width) {
            x = 0;
            y = y + kScreenWidth/5+5; //15为两行之间的高度间隔
        }
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, kScreenWidth/5, kScreenWidth/5)];
//        [button setTitle:str forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14.0f];
//        [button setImage:image forState:UIControlStateNormal];
        [button setTitleColor:kRGBColor(128, 100, 35) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(classifyListBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000+i;
//        button.backgroundColor = [UIColor purpleColor];
        
        UIImageView *btnImg = [[UIImageView alloc]initWithFrame:CGRectMake(18, 8, kScreenWidth/5-36, kScreenWidth/5-36)];
        [btnImg sd_setImageWithURL:[NSURL URLWithString:model.classify_img] placeholderImage:[UIImage imageNamed:@"jw_img_404"]];
        
        [btnImg setContentMode:UIViewContentModeScaleAspectFill];
        btnImg.clipsToBounds = YES;
        [button addSubview:btnImg];
        UILabel *btnLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, kScreenWidth/5-15, kScreenWidth/5, 15)];
        btnLabel.text = model.classify_name;
        btnLabel.font = [UIFont systemFontOfSize:14.0f];
        btnLabel.textAlignment = NSTextAlignmentCenter;
        [button addSubview:btnLabel];
        
        [_classifyListBGView addSubview:button];
        x = x + kScreenWidth/5; //10为两个标签之间的宽度间隔
        classifyListBGViewHeight = y + kScreenWidth/5+10;
        //        [_tagsBtnArr addObject:button];
    }
    _classifyListBGView.backgroundColor = [UIColor whiteColor];
    _classifyListBGView.frame = CGRectMake(0, _banner.height, kScreenWidth, classifyListBGViewHeight);
    
    UIView *sortBGView = [[UIView alloc]initWithFrame:CGRectMake(0, _banner.height+classifyListBGViewHeight, kScreenWidth, 45)];
    
//    sortBGView.backgroundColor = [UIColor purpleColor];
    UIView *topLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    topLineView.backgroundColor = kRGBColor(240, 240, 240);
    [sortBGView addSubview:topLineView];
    UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(0, sortBGView.height-1, kScreenWidth, 1)];
    bottomLineView.backgroundColor = kRGBColor(240, 240, 240);
    [sortBGView addSubview:bottomLineView];
    
    _sortBtnArr = [NSMutableArray new];
    NSArray *sortBtnArr = @[@"距离",@"人气",@"好评",@"销量"];
    for (int i = 0; i < sortBtnArr.count; i++) {
        
        UIButton *sortBtn = [[UIButton alloc]initWithFrame:CGRectMake(i*sortBGView.width/sortBtnArr.count, 0, sortBGView.width/sortBtnArr.count, sortBGView.height - topLineView.height - bottomLineView.height)];
        
        [sortBtn setTitle:sortBtnArr[i] forState:UIControlStateNormal];
        [sortBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        sortBtn.backgroundColor = [UIColor cyanColor];
        sortBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [sortBtn setImage:[UIImage imageNamed:@"jiantou_updown"] forState:UIControlStateNormal];
        [sortBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
        [sortBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 55, 0, 0)];
        [sortBtn addTarget:self action:@selector(sortBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [sortBGView addSubview:sortBtn];
        
        [_sortBtnArr addObject:sortBtn];
    }
    
    _bannerBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, _banner.height+classifyListBGViewHeight+sortBGView.height)];
    _bannerBGView.backgroundColor = [UIColor whiteColor];
    
    [_bannerBGView addSubview:_banner];
    [_bannerBGView addSubview:_classifyListBGView];
    [_bannerBGView addSubview:sortBGView];
    
    
    
    return _bannerBGView;
}
- (void)classifyListBtnOnclick:(UIButton *)sender{
    
    NSLog(@"sadfasdfasf");
}
- (void)sortBtnOnclick:(UIButton *)sender{
    
    for (UIButton *button in _sortBtnArr) {
        [button setImage:[UIImage imageNamed:@"jiantou_updown"] forState:UIControlStateNormal];
    }
    [sender setImage:[UIImage imageNamed:@"jiantou_up_icon"] forState:UIControlStateNormal];

}
-(NSMutableArray *)source_banner{
    if (!_source_banner) {
        _source_banner = [NSMutableArray array];
    }
    return _source_banner;
}
-(NSMutableArray *)classifyListArr{
    if (!_classifyListArr) {
        _classifyListArr = [NSMutableArray array];
    }
    return _classifyListArr;
}
#pragma mark 网络请求
//首页轮播图列表
-(void)HSFNet_getBannerInfo{
    [[DDNetworkManagerDate makeUrl_mall_adListWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            
//            NSLog(@"====>>%@",responseObject);
            self.source_banner = data.mutableCopy;
            kWeakSelf(self);
            __block NSMutableArray *arr = [NSMutableArray array];
            [self.source_banner enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary *dic = (NSDictionary *)obj;
                NSString *img = dic[@"ad_img"];
//                NSString *url = [NSString stringWithFormat:@"%@/upload/%@",kRequestHeaderUrl,img];
                [arr addObject:img];
            }];
            //轮播图
            self.banner.imageURLStringsGroup = arr.mutableCopy;//图片
            //block监听点击方式
            self.banner.clickItemOperationBlock = ^(NSInteger index) {
//                kStrongSelf(self);
//                NSDictionary *dic = self.source_banner[index];
//                self.hidesBottomBarWhenPushed = YES;
//                BannerDetailVC *vc = [[BannerDetailVC alloc]init];
//                //                vc.navigationController.navigationBar.hidden = NO;
//                vc.banner_id = [NSString stringWithFormat:@"%@",dic[@"id"]];
//                [self.navigationController pushViewController:vc animated:YES];
//                self.hidesBottomBarWhenPushed = NO;
            };
            
            [self makeUrl_mall_classifyList];
        }
        //刷新
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//首页分类列表
-(void)makeUrl_mall_classifyList{
    [[DDNetworkManagerDate makeUrl_mall_classifyListWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
//            NSLog(@"====>>%@",responseObject);
            _classifyListArr = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFBFacadeVCModel *model = [JWFBFacadeVCModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            _classifyListArr = arr.mutableCopy;
            self.tableView.tableHeaderView = self.bannerBGView;
        }
        //刷新
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//首页分类列表
-(void)makeUrl_mall_indexList{
    [[DDNetworkManagerDate makeUrl_mall_indexListWithtoken:k_user_token page:@"1" side:@"0" type:@"0" price:@"0,1000" shop_type:_shop_type tag_id:@"10" classify_id:@"" sort:@"0"] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            
            
            NSLog(@"====>>%@",responseObject);
           
        }
        //刷新
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
